package com.erp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.common.R;
import com.erp.entity.Cash;
import com.erp.entity.GeneralLedger;
import com.erp.service.GeneralLedgerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/generalLedger")
@RequiresRoles(value = {"FinancialAdministrator","SupperAdministrator"},logical = Logical.OR)
public class GeneralLedgerController {

    @Autowired
    private GeneralLedgerService generalLedgerService;

    /**
     * 根据id查询总账 (已测试)
     * @param id 总账id
     * @return R<GeneralLedger>
     */
    @GetMapping("/{id}")
    public R<GeneralLedger> findById(@PathVariable Long id){
        log.info("根据id查询总账");
        return R.success(generalLedgerService.getById(id));
    }

    /**
     * 新增总账信息 (已测试)
     * @param generalLedger 总账
     * @return R<String>
     */
    @PostMapping
    public R<String> save(@RequestBody GeneralLedger generalLedger){
        log.info("新增总账信息");
        boolean save = generalLedgerService.save(generalLedger);
        if (!save){
            return R.error("新增失败，类型已存在");
        }
        return R.success("成功");
    }

    /**
     * 根据id删除 (已测试)
     * @param id 总账id
     * @return R<String>
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long id){
        log.info("根据id删除总账");
        generalLedgerService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 查询全部数据 (已测试)
     * @return R<List<GeneralLedger>>
     */
    @GetMapping("/list")
    public R<List<GeneralLedger>> findAll(){
        log.info("查询总账全部数据");
        return R.success(generalLedgerService.list());
    }


    @GetMapping("/page")
    public R<Page<GeneralLedger>> page(int page , int pageSize){
        Page<GeneralLedger> cashPage = new Page<>(page,pageSize);
        return R.success(generalLedgerService.page(cashPage));
    }

}
